'''pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple'''
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression


def test1():
    df: pd.DataFrame = pd.read_csv("/home/xieweig/main/forStudent/data-hobby.csv")

    # print(df[['age','height']])
    # print(df.loc[0:11,'id'])
    #     数据预处理 准备数据集
    mydict = {'football': 1, 'basketball': 0}
    # df['label']=df['hobby'].transform(lambda x: mydict.get(x))
    df['label'] = df['hobby'].transform(mydict.get)

    height_max = df['height'].max()
    height_min = df['height'].min()
    print(height_max, height_min)
    age_max = df['age'].max()
    age_min = df['age'].min()
    print(age_max, age_min)
    df['a'] = df['height'].transform(lambda x: (x - height_min) / (height_max - height_min))
    df['b'] = df['age'].transform(lambda x: (x - age_min) / (age_max - age_min))
    # 预处理完毕
    print(df)

    # 可选的可视化处理
    # 喜欢football的
    d = df[df['label'] > 0]
    # print(d.loc[:12,'a'])
    plt.scatter(d.loc[:, 'a'], d.loc[:, 'b'], c='r')
    # 喜欢basketball的
    dd = df[df['label'] == 0]
    # print(dd)
    plt.scatter(dd.loc[:, 'a'], dd.loc[:, 'b'], c='g')

    # 训练集和测试集
    train_f = df.loc[:16, ['a', 'b']].to_numpy()
    train_l = df.loc[:16, ['label']].to_numpy()
    print(train_f,train_l)
    test_f = df.loc[17:, ['a', 'b']].to_numpy()
    test_l = df.loc[17:, ['label']].to_numpy()
    print(test_f,test_l)

    # 训练模型
    model = LogisticRegression()
    model.fit(train_f, train_l)
    print(model.coef_, model.intercept_)
    # [[-1.16197835 -1.0221022 ]] [1.19521783]
    w1,w2=model.coef_[0]
    b= model.intercept_[0]
    x_line = np.linspace(0, 1, 11)
    y_line = -(w1 * x_line + b) / w2
    plt.plot(x_line, y_line, 'r')

    # 测试集
    r = model.score(test_f, test_l)
    print(r)

    # 预测
    y_pred = model.predict_proba(test_f)
    y_pred2 = model.predict(test_f)
    print(y_pred,y_pred2)
    plt.show()

#  yolo predict model=yolo11n.pt   source='/home/xieweig/图片/report.jpeg'
# yolo predict model="yolo11n.pt" source="/home/xieweig/图片/report.jpeg"

if __name__ == '__main__':
    test1()
